package com.ruikong.dazhi.controller.base;

import com.ruikong.dazhi.controller.result.BaseResult;
import com.ruikong.dazhi.controller.result.ResultApi;
import com.ruikong.dazhi.entity.base.Permission;
import com.ruikong.dazhi.entity.base.Role;
import com.ruikong.dazhi.model.input.PermissionInput;
import com.ruikong.dazhi.model.output.PermissionOutput;
import com.ruikong.dazhi.model.output.TreeMenuOutput;
import com.ruikong.dazhi.model.output.TreeNatureOutput;
import com.ruikong.dazhi.service.base.IPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "/data")
@EnableAutoConfiguration
@Api(tags = "权限")
public class PermissionController {

    private static final Logger logger = LoggerFactory.getLogger(PermissionController.class);

    @Autowired
    private IPermissionService permissionService;

    /**
     * 获取菜单目录(两级目录)
     * @param permissionInput
     * @return
     */
    @ApiOperation(value = "获取菜单目录(两级目录)")
    @RequestMapping(value = "/permissions", method = RequestMethod.POST)
    public BaseResult getPermissionsByPersonnelId(@RequestBody PermissionInput permissionInput) {
        BaseResult result = new BaseResult();
        try {
            List<PermissionOutput> permissions = permissionService.getPermissionsByPersonnelId(permissionInput);
            result.setCode(ResultApi.successCode);
            result.setMsg(ResultApi.getSuccessMsg);
            result.setData(permissions);
        } catch (Exception e) {
            result.setCode(ResultApi.failCode);
            result.setMsg(ResultApi.getFailMsg);
            logger.error("permissions", e.getMessage());
        }
        return result;
    }


    /**
     * 获取菜单目录树
     * @return
     */
    @ApiOperation(value = "获取菜单目录树")
    @RequestMapping(value = "/getPermissionTree", method = RequestMethod.POST)
    public BaseResult getPermissionTree() {
        BaseResult result = new BaseResult();
        try {
            String data = permissionService.getPermissionTree();
            result.setCode(ResultApi.successCode);
            result.setMsg(ResultApi.getSuccessMsg);
            result.setData(data);
        } catch (Exception e) {
            result.setCode(ResultApi.failCode);
            result.setMsg(ResultApi.getFailMsg);
            logger.error("getPermissionTree", e.getMessage());
        }
        return result;
    }


    /**
     * 通过角色Id获取角色权限关联表中的权限
     * @param role
     * @return
     */
    @ApiOperation(value = "获取菜单目录树")
    @RequestMapping(value = "/getPermissionByRoleId", method = RequestMethod.POST)
    public BaseResult getPermissionByRoleId(@RequestBody Role role){
        BaseResult result = new BaseResult();
        try {
            List<Permission> permissions = permissionService .getPermissionListByRoleId(role);
            result.setCode(ResultApi.successCode);
            result.setMsg(ResultApi.getSuccessMsg);
            result.setData(permissions);
        } catch (Exception e) {
            result.setCode(ResultApi.failCode);
            result.setMsg(ResultApi.getFailMsg);
        }
        return result;
    }

    /**
     * 页面展示
     * @param role
     * @return
     */
    @ApiOperation(value = "页面展示")
    @RequestMapping(value = "/getPerByRoleId", method = RequestMethod.POST)
    public BaseResult getPerByRoleId(@RequestBody Role role){
        BaseResult result = new BaseResult();
        try {
            List<TreeMenuOutput> outputs = permissionService .getPermissionByRoleId(role);
            result.setCode(ResultApi.successCode);
            result.setMsg(ResultApi.getSuccessMsg);
            result.setData(outputs);
        } catch (Exception e) {
            result.setCode(ResultApi.failCode);
            result.setMsg(ResultApi.getFailMsg);
        }
        return result;
    }

    /**
     * 模态框展示
     * @param role
     * @return
     */
    @ApiOperation(value = "模态框展示")
    @RequestMapping(value = "/showModalTree", method = RequestMethod.POST)
    public BaseResult showModalTree(@RequestBody Role role){
        BaseResult result = new BaseResult();
        try {
            List<TreeNatureOutput> outputs = permissionService.showModalTree(role);
            result.setCode(ResultApi.successCode);
            result.setMsg(ResultApi.getSuccessMsg);
            result.setData(outputs);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(ResultApi.failCode);
            result.setMsg(ResultApi.getFailMsg);
        }
        return result;
    }

}
